<template>
	<view class="goods_detail">
		<!-- 轮播图 -->
		<swiper indicator-dots>
			<swiper-item v-for="(item, index) in swipers" :key="index">
				<image @click="previewImg(item.src)" :src="item.src"></image>
			</swiper-item>
		</swiper>
		<!-- 价格及属性 -->
		<view class="box1">
			<view class="goods_name"><b>{{info.title}}</b></view>
			<view class="price">
				<text>￥{{info.sell_price}}</text>
				<text>￥{{info.market_price}}</text>
			</view>
		</view>
		<!-- 分割线 -->
		<view class="line"></view>
		<!-- 商品属性详情 -->
		<view class="box2">
			<view>货号：{{info.goods_no}}</view>
			<view>库存：{{info.stock_quantity}}</view>
		</view>
		<!-- 分割线 -->
		<view class="line"></view>
		<!-- 商品详情介绍 -->
		<view class="box3">
			<view class="tit"><b>商品详情</b></view>
			<view class="content">
				<rich-text :nodes="content"></rich-text>
			</view>
		</view>
		<!-- 购物车底部导航组件 -->
		<view class="goods_nav">
			<uni-goods-nav :fill="true"  
			:options="options" 
			:button-group="buttonGroup"  
			@click="onClick" 
			@buttonClick="buttonClick" >
			</uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import uniGoodsNav from '@/components/uni-goods-nav/uni-goods-nav.vue'
	export default {
		data() {
			return {
				id: 0,
				swipers: [],
				info: {},
				content: '',
				// 左侧图标区域
				options: [
					{
						icon: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/uni-ui/goodsnav/kefu.png',
						text: '客服'
					}, {
						icon: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/uni-ui/goodsnav/dianpu.png',
						text: '店铺'
					}, {
						icon: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/uni-ui/goodsnav/carts.png',
						text: '购物车',
						info: 6
					},
				],
				// 右侧按钮区域
				buttonGroup: [
					{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			}
		},
		onLoad (option) {
			this.id = option.id
			this.getSwipers()
			this.getDetailInfo()
			this.getDetailContent()
		},
		components: {
			uniGoodsNav
		},
		methods: {
			// 获取轮播图数据
			async getSwipers () {
				const res = await this.$myRequest({
					url: '/api/getthumimages/' + this.id
				})
				this.swipers = res.data.message
			},
			// 获取商品信息数据
			async getDetailInfo () {
				const res = await this.$myRequest({
					url: '/api/goods/getinfo/' + this.id
				})
				this.info = res.data.message[0]
			},
			// 获取商品详情数据
			async getDetailContent () {
				const res = await this.$myRequest({
					url: '/api/goods/getdesc/' + this.id
				})
				this.content = res.data.message[0].content
			},
			// 预览图片
			previewImg (current) {
				// 用map方法将全部的图片url放入定义的数组中
				const urls = this.swipers.map(item=>{
					return item.src
				})
				// current为将点击的设为第一张预览图
				uni.previewImage({
					current,
					urls,
					indicator: 'default'
				})
			},
			// 点击按钮触发事件，可以通过索引值判断点击的是哪个按钮 
			buttonClick (e) {
				console.log(e)
			},
			onClick (e) {
				console.log(e)
			}
		}
	}
</script>

<style lang="scss">
.goods_detail{
	swiper{
		height: 700rpx;
		image{
			width: 100%;
			height: 100%;
		}
	}
	.box1 {
		padding: 10px;
		.price {
			font-size: 35rpx;
			color: $shop-color;
			line-height: 80rpx;
			text:nth-child(2){
				color: #ccc;
				font-size: 28rpx;
				text-decoration: line-through;
				margin-left: 20rpx;
			}
		}
		.goods_name{
			font-size: 32rpx;
			line-height: 60rpx;
		}
	}
	.box2 {
		color: #858585;
		padding: 0 10px;
		font-size: 28rpx;
		line-height: 70rpx;
	}
	.box3 {
		padding-bottom: 50px;
		.tit{
			font-size: 32rpx;
			padding-left: 10px;
			border-bottom: 2px solid #eee;
			line-height: 70rpx;
		}
		.content {
			padding: 10px;
			font-size: 28rpx;
			color: #333;
			line-height: 50rpx;
		}
	}
}

.goods_nav {
	position: fixed;
	bottom:0;
	width: 100%;
}

.line {
	height: 5rpx;
	width: 750rpx;
	background: #eee;
}
</style>
